<?php
require_once 'config.php';
require_once 'conndb.php';
require_once 'domain/content.domain.php';
require_once 'lib/PhpTextSpinner.inc.php';

$contentObj = new Content();

if($_POST['hEdit'] == 'Y') {
    $ID = $_POST['id'];
    $contentName = stripslashes(trim($_POST['content_name']));
    $contentValue = stripslashes(trim($_POST['content_value']));
    $contentName = htmlentities($contentName);
    $contentValue = htmlentities($contentValue);
    /*
    $spin = new PhpTextSpinner();
    $contentValue = $spin->spinContent($contentValue);
     * 
     */
    
    $contentName = base64_encode($contentName);
    $contentValue = base64_encode($contentValue);

    

    $c = new Content();
    $c->setID($ID);
    $c->setContentName($contentName);
    $c->setContentValue($contentValue);
    $contentObj->updateData($c);
    $msg = "***Update Success***";
}

if($_REQUEST['action'] == 'edit') {
    $ID = $_REQUEST['id'];
    $contentEdit = $contentObj->getByID($ID);
}

if($_REQUEST['action'] == 'del') {
    $ID = $_REQUEST['id'];
    $contentObj->deleteData($ID);
    $msg = "***Delete Success***";
}

if($_POST['hAdd'] == 'Y') {
    $contentName = stripslashes(trim($_POST['content_name']));
    $contentValue = stripslashes(trim($_POST['content_value']));
    $contentName = htmlentities($contentName);
    $contentValue = htmlentities($contentValue);

    /*
    $spin = new PhpTextSpinner();
    $contentValue = $spin->spinContent($contentValue);
     *
     */

    $contentName = base64_encode($contentName);
    $contentValue = base64_encode($contentValue);

    $c = new Content();
    $c->setID($ID);
    $c->setContentName($contentName);
    $c->setContentValue($contentValue);
    $contentObj->insertData($c);
    $msg = "***Insert Success***";
}

$contentList = $contentObj->getByObj(new Content);
?>
<?php require_once 'template/header.php'; ?>
<div style="margin: 0 auto;width: 80%;color: red;text-align: center"><?=$errMsg?></div>
<div style="margin: 0 auto;width: 80%;color: green;text-align: center"><?=$msg?></div>
<fieldset style="background: #F2F2F2;"><legend><h3>Template Manage</h3></legend>

    <div id="add" style="width: 90%;margin: 0 auto;background: #FFF;padding: 10px">
        <fieldset>
            <legend>
                <h3>
                    <?php if($_REQUEST['action'] != 'edit') { ?>
                    Add Content
                        <?php } elseif($_REQUEST['action'] == 'edit') { ?>
                    Edit Content
                    <a href="#" onclick="window.open('content.php','_parent');"><<< Back To Add</a>
                        <?php } ?>
                </h3>
            </legend>
            <form id="form" action="content.php"  method="post">
                <table>
                    <tr>
                        <td>Content Name : </td>
                        <td>
                            <input type="text" id="content_name" name="content_name" size="50"
                                   value="<?php echo base64_decode($contentEdit[0]['content_name']); ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td>Content Value : </td>
                        <td>
                            <textarea name="content_value" cols="80" rows="15"><?php echo base64_decode($contentEdit[0]['content_value']); ?></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>
                            <?php if($_REQUEST['action'] != 'edit') { ?>
                            <input type="submit" value="Add Content" />
                            <input type="hidden" name="hAdd" value="Y" />
                                <?php } elseif($_REQUEST['action'] == 'edit') { ?>
                            <input type="hidden" name="id" value="<?php echo $contentEdit[0]['ID']; ?>" />
                            <input type="hidden" name="hEdit" value="Y" />
                            <input type="hidden" name="action" value="edit" />
                            <input type="submit" value="Edit Content" />
                                <?php } ?>
                        </td>
                    </tr>
                </table>
            </form>
        </fieldset>
    </div>

    <div id="demo"  style="width: 90%;margin: 0 auto;background: #FFF;padding: 10px;margin-top: 10px;">
        <table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Content Name</th>
                    <th>Delete</th>
                </tr>
            </thead>
            <tbody>
                <?php for ($i = 0; $i < count($contentList); $i++) { ?>
                <tr onclick="edit('<?php echo $contentList[$i]['ID']; ?>')">
                    <td><?php echo $contentList[$i]['ID']; ?></td>
                    <td><?php echo base64_decode($contentList[$i]['content_name']); ?></td>
                    <td align="center"><a href="?id=<?php echo $contentList[$i]['ID']; ?>&action=del">Delete</a></td>
                </tr>
                    <? } ?>
            </tbody>
            <tfoot>
                <tr>
                    <th>ID</th>
                    <th>Content Name</th>
                </tr>
            </tfoot>
        </table>
    </div>
</fieldset>

<?php require_once 'template/footer.php'; ?>
<?php require_once 'closedb.php' ?>;
<script type="text/javascript">
    function edit(id) {
        window.location = "?id=" + id + "&action=edit";
    }

    $(document).ready(function() {

        $("#form").validate({
            rules: {
                content_name: "required",
                content_value: "required"
            }
        });
    });
</script>

